package com.atguigu.guli.service.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.atguigu.guli.service.oss.service.FileService;
import com.atguigu.guli.service.oss.util.OssProperties;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private OssProperties ossProperties;

    private String endpoint/* = ossProperties.getEndpoint()*/;
    //        System.out.println("endpoint = " + endpoint);
    private String keyid/* = ossProperties.getKeyId()*/;
    //        System.out.println("keyid = " + keyid);
    private String keysecret/* = ossProperties.getKeySecret()*/;
    //        System.out.println("keysecret = " + keysecret);
    private String bucketname/* = ossProperties.getBucketName()*/;
//        System.out.println("bucketname = " + bucketname);


    @PostConstruct
    public void init(){
        endpoint = ossProperties.getEndpoint();
        keyid = ossProperties.getKeyId();
        keysecret = ossProperties.getKeySecret();
        bucketname = ossProperties.getBucketName();
    }

    @Override
    public String upload(InputStream inputStream, String module, String oFilename) {

//        String endpoint = ossProperties.getEndpoint();
        System.out.println("endpoint = " + endpoint);
//        String keyid = ossProperties.getKeyId();
        System.out.println("keyid = " + keyid);
//        String keysecret = ossProperties.getKeySecret();
        System.out.println("keysecret = " + keysecret);
//        String bucketname = ossProperties.getBucketName();
        System.out.println("bucketname = " + bucketname);
        //判断oss实例是否存在：如果不存在则创建，如果存在则获取
        OSS ossClient = new OSSClientBuilder().build(endpoint, keyid, keysecret);
        if (!ossClient.doesBucketExist(bucketname)) {
            //创建bucket
            ossClient.createBucket(bucketname);
            //设置oss实例的访问权限：公共读
            ossClient.setBucketAcl(bucketname, CannedAccessControlList.PublicRead);
        }

        //构建日期路径：avatar/2019/02/26/文件名
        String folder = new DateTime().toString("yyyy/MM/dd");

        //文件名：uuid.扩展名
        String fileName = UUID.randomUUID().toString();
        String fileExtension = oFilename.substring(oFilename.lastIndexOf("."));
        String key = module + "/" + folder + "/" + fileName + fileExtension;

        //文件上传至阿里云
        ossClient.putObject(ossProperties.getBucketName(), key, inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();

        //返回url地址
        return "https://" + bucketname + "." + endpoint + "/" + key;
    }

    @Override
    public void removeFile(String url) {
//        String endpoint = ossProperties.getEndpoint();
        System.out.println("endpoint = " + endpoint);
//        String keyid = ossProperties.getKeyId();
        System.out.println("keyid = " + keyid);
//        String keysecret = ossProperties.getKeySecret();
        System.out.println("keysecret = " + keysecret);
//        String bucketname = ossProperties.getBucketName();
        System.out.println("bucketname = " + bucketname);

        OSS ossClient = new OSSClientBuilder().build(endpoint,keyid,keysecret);
        String host = "https://" + bucketname + "." + endpoint + "/";
        String objectName = url.substring(host.length());

        // 删除文件。
        ossClient.deleteObject(bucketname, objectName);

        // 关闭OSSClient。
        ossClient.shutdown();
    }
}